package com.example.servicea.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.SqlHelper;
import com.baomidou.mybatisplus.plugins.Page;
import com.example.servicea.dao.UserMapper;
import com.example.servicea.dao.UserRoleMapper;
import com.example.servicea.entity.UserEntity;
import com.example.servicea.entity.UserRoleEntity;
import com.example.servicea.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @Author generator.
 * @Date: 2017/10/26 10:58
 */
@Service("userServiceImpl")
public class UserServiceImpl implements UserService {
	@Autowired
    private UserMapper userMapper;

    @Override
    public String insert(UserEntity user){
        userMapper.insert(user);
        return user.getId()+"";
    }

    @Override
    public List<UserEntity> selectList(UserEntity user){
        EntityWrapper<UserEntity> query = new EntityWrapper<>();
		return userMapper.selectList(query);
	}

    @Override
    public Page selectPage(UserEntity user, String page, String limit){

        EntityWrapper<UserEntity> query = new EntityWrapper<>();
        if(StringUtils.isNotBlank(user.name)){
            query.like("name",user.name);
        }
        List<UserEntity> list = userMapper.selectPage(
                new Page<UserEntity>(Integer.parseInt(page),Integer.parseInt(limit)),
                query);
        int total = userMapper.selectCount(query);
        Page UserEntityPage = new Page();
        return UserEntityPage;
	}
    @Override
    public int selectCount(UserEntity user){
        EntityWrapper<UserEntity> query = new EntityWrapper<>();
        return userMapper.selectCount(query);
	}

    @Override
    public UserEntity selectOne(UserEntity user){
        return userMapper.selectOne(new UserEntity());
	}

    @Override
    public UserEntity selectById(String id){
        return userMapper.selectById(id);
	}

	@Override
    @Transactional(rollbackFor = Exception.class)
    public String updateById(UserEntity user){
        userMapper.updateById(user);
        return user.getId()+"";
	}

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(String id){
        return SqlHelper.retBool(userMapper.deleteById(Integer.parseInt(id)));
	}

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatchIds(String ids){
        String[] s = ids.split(",");
        List<String> list = Arrays.asList(s);
        return SqlHelper.retBool(userMapper.deleteBatchIds(list));
	}
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Override
    public boolean insertRoles(String roleIds0,String userId) {
        String[] roleIds = roleIds0.split(",");
        if(roleIds.length>0){
            userRoleMapper.delete(new EntityWrapper<UserRoleEntity>().eq("userId",userId));
            for (int i = 0; i < roleIds.length; i++) {
                UserRoleEntity obj = new UserRoleEntity();
                obj.setRoleId(roleIds[i]);
                obj.setUserId(userId);
                boolean res = SqlHelper.retBool(userRoleMapper.insert(obj));
                if(!res) {return false;}
            }
        }
        return true;
    }

}
